home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / support / destroydb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  3.4 KB  |  193 lines

  1. # include    <stdio.h>
  2. # include    <sys/types.h>
  3. # include    <dirent.h>
  4. # include    <ingres.h>
  5. # include    <aux.h>
  6. # include    <access.h>
  7.  
  8. /*
  9. **  DESTROY DATA BASE
  10. **
  11. **    This program destroys an existing database.  To be able
  12. **    to wield this awesome power, you must be the dba for
  13. **    the database.  Also, anyone has this power if the admin
  14. **    the database, or the ingres superuser, and have the "-s"
  15. **    flag requested.  If admin is trashed, the INGRES superuser
  16. **    must either destroy the database or recover it.
  17. **
  18. **    If -m is specified, then the directory is not removed.
  19. **    This is useful if the directory is a mounted file system.
  20. */
  21.  
  22. extern char    *Usercode;
  23. extern int    Status;
  24. extern char    *Pathname;
  25. extern char    *Parmvect[];
  26. extern char    *Flagvect[];
  27. extern char    *Dbpath;
  28. struct admin    Admin;
  29. short        tTdbu[100];
  30.  
  31. main(argc, argv)
  32. int    argc;
  33. char    *argv[];
  34. {
  35.     register int    i;
  36.     register char    *dbase;
  37.     int        superuser, mounted;
  38.     char        **av;
  39.     register char    *p;
  40.     char        *q;
  41.     DIR        *dirp;
  42.  
  43.     argv[argc] = NULL;
  44. #    ifdef xSTR1
  45.     tTrace(argv, 'T', tTdbu, 100);
  46. #    endif
  47.     
  48.     i = initucode(argc, argv, TRUE, NULL, -1);
  49.     dbase = Parmvect[0];
  50. #    ifdef xSTR1
  51.     if (tTf(1, 0))
  52.     {
  53.         printf("after initcode %d: ", i);
  54.         prargs(argc, argv);
  55.     }
  56. #    endif
  57.     switch (i)
  58.     {
  59.       case 0:
  60.       case 5:
  61.         break;
  62.  
  63.       case 1:
  64.       case 6:
  65.         printf("Database %s does not exist\n", dbase);
  66.         exit(-1);
  67.  
  68.       case 2:
  69.         printf("You are not authorized to access database %s\n", dbase);
  70.         exit(-1);
  71.  
  72.       case 3:
  73.         printf("You are not an authorized INGRES user\n");
  74.         exit(-1);
  75.  
  76.       case 4:
  77.         printf("No database name specified\n");
  78.     usage:
  79.         printf("Usage: destroydb [-s] [-m] dbname\n");
  80.         exit(-1);
  81.  
  82.       default:
  83.         syserr("initucode %d", i);
  84.     }
  85.  
  86.     mounted = superuser = 0;
  87.     for (av = Flagvect; (p = *av) != NULL; av++)
  88.     {
  89. #        ifdef xSTR1
  90.         if (tTf(1, 1))
  91.             printf("p = *av (\"%s\")\n", p);
  92. #        endif
  93.         if (p[0] != '-')
  94.         {
  95.         badflag:
  96.             printf("Bad flag %s\n", p);
  97.             goto usage;
  98.         }
  99.         switch (p[1])
  100.         {
  101.  
  102.           case 's':
  103.             superuser++;
  104.             break;
  105.  
  106.           case 'm':
  107.             mounted++;
  108.             break;
  109.  
  110.           default:
  111.             goto badflag;
  112.         }
  113.     }
  114.  
  115.     if (Parmvect[1] != NULL)
  116.     {
  117.         printf("Too many parameters to destroydb\n");
  118.         goto usage;
  119.     }
  120.     if (length(dbase) > 14)
  121.         syserr(0, "invalid dbname %s", dbase);
  122.     if (superuser && (Status & U_SUPER) == 0)
  123.         syserr(0, "you may not use the -s flag");
  124.  
  125.     if (!superuser)
  126.     {
  127.         if (!bequal(Admin.adhdr.adowner, Usercode, UCODE_SZ))
  128.         {
  129.             printf("You are not the DBA for %s\n", dbase);
  130.             exit(-1);
  131.         }
  132.     }
  133.  
  134.     if (chdir(Dbpath) < 0)
  135.         syserr("chdir %s", Dbpath);
  136.  
  137.     if ( (dirp = opendir(".")) == NULL )
  138.         syserr("Can't open . in %s",Dbpath);
  139.     clean(dirp);
  140.     closedir(dirp);
  141.  
  142.     if (!mounted)
  143.     {
  144.         /* find end of Dbpath and trim it off. */
  145.         for (p = q = Dbpath; *p != '\0'; p++)
  146.             if (*p == '/')
  147.                 q = p;
  148.         *q++ = '\0';
  149.         if (chdir(Dbpath) < 0)
  150.             syserr("chdir(%s)", Dbpath);
  151.         if ( i == 5 )
  152.             if ( unlink(ztack(ztack(Pathname,"/data/base/"),dbase)) == -1 )
  153.                 syserr("Can't unlink the indirect file %s",dbase);
  154.         if (rmdir(dbase)) 
  155.             perror("removing database");
  156.     }
  157. }
  158.  
  159.  
  160.  
  161. clean(dirp)
  162. register DIR    *dirp;
  163. {
  164.     struct    dirent    *dp;
  165.  
  166. #    ifdef xSTR1
  167.     if (tTf(2, 0))
  168.         printf("clean: ");
  169. #    endif
  170.  
  171.     for ( dp = readdir(dirp) ; dp != NULL ; dp = readdir(dirp) )
  172.     {
  173.         if ( !strcmp(".",dp->d_name) || !strcmp("..",dp->d_name) )
  174.             continue;
  175. #        ifdef xSTR1
  176.         if (tTf(2, 1))
  177.             printf("unlinking %s\n", dp->d_name);
  178. #        endif
  179.         unlink(dp->d_name);
  180.     }
  181. }/* clean */
  182.         
  183.  
  184. /*
  185. **  Rubout processing.
  186. */
  187.  
  188. void
  189. rubproc()
  190. {
  191.     exit(-2);
  192. }
  193.